package com.haixiaoke.flat.lock.controller;

import java.util.List;

import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.lock.domain.LockFace;
import com.haixiaoke.saas.lock.service.ILockFaceService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 锁人脸授权Controller
 * 
 * @author panda
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/front/flat/lock/lockFace")
public class FlatLockFaceController extends BaseController {
    @Autowired
    private ILockFaceService lockFaceService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 查询锁人脸授权列表
     */
    @GetMapping("/page")
    public TableDataInfo page(LockFace lockFace) {
        if (lockFace.getLockId() == null) {
            return getDataTable("请选择锁");
        }
        startPage();
        List<LockFace> list = lockFaceService.selectLockFaceList(lockFace);
        return getDataTable(list);
    }

    /**
     * 查询锁人脸授权列表
     */
    @GetMapping("/list")
    public AjaxResult list(LockFace lockFace) {
        List<LockFace> list = lockFaceService.selectLockFaceList(lockFace);
        return success(list);
    }

    /**
     * 获取锁人脸授权详细信息
     */
    @GetMapping(value = "/{faceId}")
    public AjaxResult getInfo(@PathVariable("faceId") Long faceId) {
        return success(lockFaceService.selectLockFaceByFaceId(faceId));
    }

    /**
     * 新增锁人脸授权
     */
    @AuthCheck
    @PostMapping
    public AjaxResult add(@RequestBody LockFace lockFace) {
        return lockFaceService.insertLockFace(lockFace, frontTokenComponent.getUserIdNotNull());
    }


    /**
     * 人脸授权禁用/启用
     */
    @AuthCheck
    @PutMapping("/editFaceState/{faceId}")
    public AjaxResult editFaceState(@PathVariable("faceId") Long faceId) {
        return lockFaceService.editFaceState(faceId, frontTokenComponent.getUserIdNotNull());
    }

    /**
     * 修改锁人脸授权
     */
    @AuthCheck
    @PutMapping
    public AjaxResult edit(@RequestBody LockFace lockFace) {
        return lockFaceService.updateLockFace(lockFace, frontTokenComponent.getUserIdNotNull());
    }

    /**
     * 修改锁的主管理员
     */
    @AuthCheck
    @PutMapping("/mainAdmin")
    public AjaxResult mainAdmin(@RequestBody LockFace lockFace) {
        return lockFaceService.updateLockMainAdmin(lockFace, frontTokenComponent.getUserIdNotNull());
    }

    /**
     * 删除锁人脸授权
     */
    @AuthCheck
	@DeleteMapping("/{faceId}")
    public AjaxResult remove(@PathVariable Long faceId) {
        return lockFaceService.deleteLockFaceByFaceId(faceId, frontTokenComponent.getUserIdNotNull(), frontTokenComponent.getLoginUser().getUser().getUserName());
    }
}
